package util;

import java.sql.*;

import static java.lang.Class.forName;

public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/std-grade?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8";
    //private static final String URL = "jdbc:mysql://localhost:3306/std-grade";
    //private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    //private static final String DRIVER = "Class.forName(/"com.mysql.cj.jdbc.Driver")";
    private static final String User_name = "root";

    private static final String pwd = "123456";

    static {
        try {
            forName(DRIVER);
        } catch (ClassNotFoundException e) {
            //e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static Connection getConn() {
        try {
            return DriverManager.getConnection(URL, User_name, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取数据库链接
    public static void closeConn(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void closePs(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static void closeRs(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
